package com.example.raidball;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;

public class BounceView extends View {

	/* Our Ball together with the location it will be painted */
	protected Drawable mySprite;
	protected Point mySpritePos;
	private createWorld inst_createWorld;
	
	
	
	
	
	Rectangle ball;
	
	protected Point fencespritePos = new Point(250, 250);
	
	//private LoadWorld map;
	
	int SaveX, SaveY;
	static float mRoll, mPitch, mHeading;
	
	/*
	 * Working with a Enum is 10000% safer than working with int's to 'remember'
	 * the direction.
	 */
	protected enum HorizontalDirection {
		LEFT, RIGHT
	}
	protected enum VerticalDirection {
		UP, DOWN
	}
	protected HorizontalDirection myXDirection = HorizontalDirection.RIGHT;
	protected VerticalDirection myYDirection = VerticalDirection.UP;
	
	/**
	 * Constructeur
	 * @param context
	 */
	@SuppressWarnings("deprecation")
	public BounceView(Context context, int numLevel) {
		super(context);
		
		// Set the background
		if(numLevel==0){
			this.setBackgroundDrawable(
					this.getResources().getDrawable(Levels.R_VALUE_WALL_101
							));
			
		}
		if(numLevel==1){
			this.setBackgroundDrawable(
					this.getResources().getDrawable(Levels.R_VALUE_WALL_102
							));
			
		}
		if(numLevel==2){
			this.setBackgroundDrawable(
					this.getResources().getDrawable(Levels.R_VALUE_WALL_103
							));
		}
		
		inst_createWorld = new createWorld(context, this, numLevel);
		
		// Creation des list de cases (rect_list , collision_list)
		
		
					
		

		
		// positionne la bille sur la case depart du niveau, sinon a 0,0.
		mySpritePos = inst_createWorld.getStartPos();
		
		// Fabrique l'object Rectangle de la BILLE "Ball"
		this.mySprite = this.getResources().getDrawable(R.drawable.pball);
		ball = new Rectangle(25, 25, mySpritePos.x, mySpritePos.y, mySprite, 0);
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
	
		
		// redessine le tableau des cases {mur|vide|start|finish}
		inst_createWorld.drawKetchose(canvas);

		/*
		 * L'axe Y donne l'axe verticale L'axe X donne l'axe horizontale
		 */
		SaveX = mySpritePos.x;
		SaveY = mySpritePos.y;

		/*
		 * Check if the Ball started to leave the screen on left or right side
		 */
		this.mySpritePos.x -= mPitch;

		if (mySpritePos.x >= this.getWidth() - mySprite.getBounds().width()) {
			this.myXDirection = HorizontalDirection.LEFT;
			this.mySpritePos.x = this.getWidth() - mySprite.getBounds().width();
			vibrate();
		} else if (mySpritePos.x <= 0) {
			this.myXDirection = HorizontalDirection.RIGHT;
			this.mySpritePos.x = 0;
			vibrate();
		}

		/*
		 * Check if the Ball started to leave the screen on bottom or upper side
		 */
		this.mySpritePos.y += mHeading;
		if (mySpritePos.y >= this.getHeight() - mySprite.getBounds().height())
		{
			this.myYDirection = VerticalDirection.UP;
			this.mySpritePos.y = this.getHeight()
					- mySprite.getBounds().height();
			vibrate();
		} else if (mySpritePos.y <= 0)
		{
			this.myYDirection = VerticalDirection.DOWN;
			this.mySpritePos.y = 0;
			vibrate();
		}

		ball.bottom = mySpritePos.x + 50;
		ball.left = mySpritePos.y;
		ball.top = mySpritePos.x;
		ball.right = mySpritePos.x + 50;

		
		// collision des collision_list (disponible dans la class createWorld).
		collision2(ball);
	
		
		// possition et dessin de la BILLE (pball.png init dans le constructeur)
		this.mySprite.setBounds(this.mySpritePos.x, this.mySpritePos.y,
				this.mySpritePos.x + 50, this.mySpritePos.y + 50);

		/* Make the sprite draw itself to the canvas */
		this.mySprite.draw(canvas);

		}


	public boolean collision2(Rectangle ball)
	{
		for(Rectangle rect : inst_createWorld.getRectList())
		{
			if(ball.intersect(rect))
			{
				
				//Log.d("Test","La bille est dans le rectangle, en X " +rect.posX+" en Y: "+rect.posY);	
				for(Rectangle rect2 : inst_createWorld.getCollisionList())
				{
	
					if (rect2.getIdBlock() == 1) //mur ?
					{
						//mySpritePos.x=SaveX;
					    //mySpritePos.y=SaveY;
						

						Log.d("test", "BOOM - ID BLOCK : " +rect2.getIdBlock());
						
						if(rect2.posX==rect.posX && rect2.posY==rect.posY+50)
						{
							Log.d("test", "Collision1 - Ball :" + mySpritePos.x
									+ " - Mur = " + rect2.posX);
							Log.d("test", "Collision2 - Ball :" + mySpritePos.y
									+ " - Mur = " + rect2.posY);
							mySpritePos.x=SaveX;
							mySpritePos.y=SaveY;
							return true;
					   }
					   if(rect2.posX==rect.posX+50 && rect2.posY==rect.posY+50)
					   {
						   Log.d("test", "Collision1 - Ball :" + mySpritePos.x
									+ " - Mur = " + rect2.posX);
							Log.d("test", "Collision2 - Ball :" + mySpritePos.y
									+ " - Mur = " + rect2.posY);
							mySpritePos.x=SaveX;
							mySpritePos.y=SaveY;
							return true;
					   }
					   if(rect2.posX==rect.posX+50 && rect2.posY==rect.posY)
					   {
						   Log.d("test", "Collision1 - Ball :" + mySpritePos.x
									+ " - Mur = " + rect2.posX);
							Log.d("test", "Collision2 - Ball :" + mySpritePos.y
									+ " - Mur = " + rect2.posY);
							mySpritePos.x=SaveX;
							mySpritePos.y=SaveY;
							return true;
					   }
					   if(rect2.posX==rect.posX && rect2.posY==rect.posY-50)
					   {
							Log.d("test", "Collision1 - Ball :" + mySpritePos.x
									+ " - Mur = " + rect2.posX);
							Log.d("test", "Collision2 - Ball :" + mySpritePos.y
									+ " - Mur = " + rect2.posY);
							mySpritePos.x=SaveX;
							mySpritePos.y=SaveY;
							return true;
					   }
					   
					//}
					/*
					if (rect2.getIdBlock() == 2)
					{
						
					}
					if (rect2.getIdBlock() == 2)
					{
						
					}*/}
				
				}
				
			
		}
		
		}
		return false;
	}


	public void vibrate() {

		Vibrator v = (Vibrator) getContext().getSystemService(
				Context.VIBRATOR_SERVICE);
		// Vibrate for 250 milliseconds
		v.vibrate(250);
	}

}